package cn.zzp.order.controller;

import cn.zzp.basic.util.JsonResult;
import cn.zzp.basic.util.LoginContext;
import cn.zzp.basic.util.PageList;
import cn.zzp.order.domain.OrderPetAcquisition;
import cn.zzp.order.dto.OrderDto;
import cn.zzp.order.query.OrderPetAcquisitionQuery;
import cn.zzp.order.service.OrderPetAcquisitionService;
import cn.zzp.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName OrderPetAcquisitionController
 * @Description
 * @Author 祝志鹏
 * @Date 2022/8/11 11:50
 */
@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {

    @Autowired
    private OrderPetAcquisitionService orderPetAcquisitionService;

    /**
     * 分页查询
     * @return
     */
    @PostMapping
    public PageList<OrderPetAcquisition> pageQuery(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request){

        try {
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            return orderPetAcquisitionService.pageQuery(query, loginInfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<OrderPetAcquisition>();
        }

    }

    /**
     * 订单确认
     * @return
     */
    @PostMapping("/confirm")
    public JsonResult confirm(@RequestBody OrderDto orderDto) {
        try {
            orderPetAcquisitionService.confirm(orderDto);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult cancel(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.cancel(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }
}
